package com.cn.producer;

import com.cn.constant.TopicConst;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.support.SendResult;
import org.springframework.stereotype.Service;
import org.springframework.util.concurrent.ListenableFutureCallback;

/**
 * 类的功能描述：
 * 消息生产者用于发送消息
 */
@Service
public class MessageProducer
{
    private final Logger log = LoggerFactory.getLogger(MessageProducer.class);

    @Autowired
    KafkaTemplate kafkaTemplate;

    public void send(String payMessage)
    {
        kafkaTemplate.send(TopicConst.PAY_TOPIC, payMessage).addCallback(new ListenableFutureCallback<SendResult<String, Object>>()
        {
            @Override
            public void onFailure(Throwable ex)
            {
                log.info("发送消息失败：" + ex.getMessage());
            }

            @Override
            public void onSuccess(SendResult<String, Object> result)
            {
                log.info("发送消息成功：" + result.getRecordMetadata().topic() + "-"
                        + result.getRecordMetadata().partition() + "-" + result.getRecordMetadata().offset());
            }
        });
    }

}